import { reactive } from 'vue'
import { getArrayKey } from '/@/utils/common'
import { i } from 'vite/dist/node/types.d-aGj9QkWt'
export default class  baTableNew{
    public table: BaTableNew = reactive({
            // 表格加载状态
            loading: false,
            // 表格列定义
            column: [],
            // 表格数据，通过getIndex加载
            data: [],
            // 默认排序字段和排序方式
            defaultOrder:  { prop: "", order: "" },
            //表格上是否显示公用查询
            showComSearch: false,
            //表格是否显示工具条
            tableButtons:[],
            // 分页配置
            pagination: undefined
    })
    //分页默认配置
    public tablePagination: TablePagination = reactive({
        //每页显示条数
        pageSize: 10,
        //当前页
        currentPage: 1,
        //总条数
        total: 0,
        //分布布局
        layout: "sizes, prev, pager, next, jumper, ->, total, slot",
        //分页显示的页码数量
        pageSizes: [10, 50, 100, 200],
    })

    constructor(table: BaTableNew = {},pagination : TablePagination = {}) {
         //初始化分页配置
        this.table = Object.assign(this.table, table);
        if(pagination){
            this.table.pagination =  Object.assign(this.tablePagination, pagination);
        }
        //this.tablePagination = Object.assign(this.tablePagination, pagination);
        //初始化表格
        this.initTable();
    }


    /**
     * 初始化表格
     */
    initTable() {
        for (let item of this.table.column ?? []) {
             //表格列默认显示
            item.show = item.show ?? true
        }
    }

    /**
     * 表格顶栏按钮事件统一响应
     * @param event 事件:change-show-column=调整列显示状态
     * @param data 携带数据
     */
    onTableHeaderAction = (event: string, data: anyObj) => {
        const actionFun = new Map([
            [
                'change-show-column',
                () => {
                    if(this.table.column){
                        const columnKey = getArrayKey(this.table.column, 'prop', data.field)
                        this.table.column[columnKey].show = data.value
                    }
                },
            ],
            [
                'default',
                () => {
                    console.warn('No action defined')
                },
            ],
        ])
        const action = actionFun.get(event) || actionFun.get('default')
        action!.call(this)
    }
}
